// vip/withdraw/index.js
import {toast} from '../../utils/util'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: 'reflectSetValue',
    payment: 0,
    balance: 0,
    price: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCode()
    this.initBalance()
  },
  initBalance() {
    const {balance} = wx.getStorageSync('userInfo')
    this.setData({
      balance
    })
  },
  getCode() {
    const {code} = this.data
    app.$api.common.commonFindName({
      code
    }).then((res) => {
      const data = res.data
      console.log(data)
      this.setData({
        payment: data.name
      })
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  },
  bindPrice(e) {
    const price = e.detail.value
    this.setData({
      price
    })
  },
  bindWithdarw() {
    const withdrawamount = this.data.price
    const flag = this.validate()
    if(flag) {
      app.$api.user.withdraw({withdrawamount})
        .then((res) => {
          const data = res.data
          toast('提现成功', 1000)
        }).catch((e) => {
          toast('请稍后再试', 1000)
        })
    }
  },
  validate() {
    const {price,payment,balance} = this.data
    if(balance < payment ) {
      wx.showModal({
        title: '温馨提示',
        showCancel: false,
        content: `最低提现额度为${payment}`
      })
      return false
    }
    if(price === 0 || price < payment) {
      wx.showModal({
        title: '温馨提示',
        showCancel: false,
        content: `最低提现额度为${payment}`
      })
      return false
    }
    if(price > balance) {
      wx.showModal({
        title: '温馨提示',
        showCancel: false,
        content: `您可提现额度为${balance}`
      })
      return false
    }
    return true
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})